﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestorDocument.Model.IRepository;
using System.Collections.ObjectModel;
using GestorDocument.Model;

namespace GestorDocument.ViewModel
{
    public class ReporteAsuntosPendientesViewModel : ViewModelBase
    {
        #region PROPIEDADES
        // ***************************** ***************************** *****************************
        // CARGAR COMBO DESTINATARIOS.
        private IOrganigrama _OrganigramaRepository;
        public ObservableCollection<OrganigramaModel> dataListDestinatarios
        {
            get { return _dataListDestinatarios; }
            set
            {
                if (_dataListDestinatarios != value)
                {
                    _dataListDestinatarios = value;
                    OnPropertyChanged(dataListDestinatariosPropertyName);
                }
            }
        }
        private ObservableCollection<OrganigramaModel> _dataListDestinatarios;
        public const string dataListDestinatariosPropertyName = "dataListDestinatarios";


        public OrganigramaModel SelectedDestinatario
        {
            get { return _SelectedDestinatario; }
            set
            {
                if (_SelectedDestinatario != value)
                {
                    _SelectedDestinatario = value;
                    OnPropertyChanged(SelectedDestinatarioPropertyName);
                }
            }
        }
        private OrganigramaModel _SelectedDestinatario;
        public const string SelectedDestinatarioPropertyName = "SelectedDestinatario";

        public string SelectedDest
        {
            get { return _SelectedDest; }
            set
            {
                if (_SelectedDest != value)
                {
                    _SelectedDest = value;
                    OnPropertyChanged(SelectedDestPropertyName);
                }
            }
        }
        private string _SelectedDest;
        public const string SelectedDestPropertyName = "SelectedDest";

        public string SelectedDestinatarioName
        {
            get { return _SelectedDestinatarioName; }
            set
            {
                if (_SelectedDestinatarioName != value)
                {
                    _SelectedDestinatarioName = value;
                    OnPropertyChanged(SelectedDestinatarioNamePropertyName);
                }
            }
        }
        private string _SelectedDestinatarioName;
        public const string SelectedDestinatarioNamePropertyName = "SelectedDestinatarioName";

        public List<long> IdDestinatarios
        {
            get { return _IdDestinatarios; }
            set
            {
                if (_IdDestinatarios != value)
                {
                    _IdDestinatarios = value;
                    OnPropertyChanged(IdDestinatariosPropertyName);
                }

            }
        }
        private List<long> _IdDestinatarios;
        public const string IdDestinatariosPropertyName = "IdDestinatarios";
        #endregion

        #region IsCheks

        private bool _IsTodosDestinatarios;
        public bool IsTodosDestinatarios
        {
            get { return _IsTodosDestinatarios; }
            set
            {
                if (_IsTodosDestinatarios != value)
                {
                    _IsTodosDestinatarios = value;

                    try
                    {
                        (from d in this.dataListDestinatarios
                         select d).ToList().ForEach(d => d.IsChecked = _IsTodosDestinatarios);
                        if (value)
                            AllDestinatarios();
                        else
                            this.SelectedDestinatarioName = null;
                    }
                    catch (System.Exception)
                    {
                        ;
                    }
                    OnPropertyChanged(IsTodosDestinatariosPropertyName);

                }
            }
        }
        private const string IsTodosDestinatariosPropertyName = "IsTodosDestinatarios";

        #endregion

        #region BOTON FILTRO
        // ***************************** ***************************** *****************************
        // VALIDA BUSQUEDA.

        public RelayCommand ValidarSearchCommand
        {
            get
            {
                if (_ValidarSearchCommand == null)
                {
                    _ValidarSearchCommand = new RelayCommand(p => this.ValidarAttemptSearch(), p => this.ValidarCanSearch());
                }

                return _ValidarSearchCommand;
            }

        }
        private RelayCommand _ValidarSearchCommand;
        public bool ValidarCanSearch()
        {
            bool _CanSearch = false;
            this.AllDestinatarios();
            

            if (!String.IsNullOrEmpty(this.SelectedDestinatarioName))
            {
                _CanSearch = true;
            }

            return _CanSearch;
        }
        public void ValidarAttemptSearch()
        {
            //solo valida
        }
        #endregion

        #region CONSTRUCTOR
        // ***************************** ***************************** *****************************
        // CONSTRUCTOR.
        public ReporteAsuntosPendientesViewModel()
        {
            this._OrganigramaRepository = new GestorDocument.DAL.Repository.OrganigramaRepository();
            this.dataListDestinatarios = new ObservableCollection<OrganigramaModel>();
            this.LoadInfo();
        }
        #endregion

        #region METODOS
        public void LoadInfo()
        {
            ObservableCollection<OrganigramaModel> res = this._OrganigramaRepository.GetOrganigrama() as ObservableCollection<OrganigramaModel>;

            res.ToList().Where(r => r.IdJerarquiaParent == 10 && r.IdJerarquia !=1)
                        .OrderBy( o=> o.JerarquiaName).ToList()
                        .ForEach(p => this.dataListDestinatarios.Add(p));
        }

        public void GetDestinatarios()
        {
            this.IdDestinatarios = new List<long>();
            try
            {
                this.IdDestinatarios = (from o in this.dataListDestinatarios
                                        where o.IsChecked == true
                                        select o.IdJerarquia).ToList();
            }
            catch (Exception)
            {
            }
            this.SelectedDest = null;
            if (this.IdDestinatarios.Count != 0)
            {
                this.SelectedDest = String.Join(",", this.IdDestinatarios);
            }
        }

        private void AllDestinatarios()
        {
            var res = (from d in this.dataListDestinatarios
                       where d.IsChecked == true
                       select d).ToList();
            string destinatarios = String.Join(",", res.Select(item => item.JerarquiaName.ToString()).ToList());
            this.SelectedDestinatarioName = destinatarios;
        }
        #endregion
    }
}
